/**
 * 2011-3-27上午11:06:31Login.java
 * @author 刘杰 
 */
package com.lyjilu.web.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.lyjilu.model.entity.PopUser;
import com.lyjilu.model.service.IPopUserService;

/**
 * @author Administrator
 * 
 */
@Controller
public class Login {
	private IPopUserService popUserService;

	public void setPopUserService(IPopUserService popUserService) {
		this.popUserService = popUserService;
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(PopUser user) {
		PopUser popUser = null;
		try {
			popUser = popUserService.login(user.getUsername());
			System.out.println(user.getUsername());
		} catch (Exception e) {
			System.out.println("login:=");
			e.printStackTrace();
		}
		String res = "";
		if (popUser != null && popUser.getPassword().equals(user.getPassword())) {
			res = "index";
		} else {
			res = "login";
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", new PopUser());
		ModelAndView mav = new ModelAndView(res, map);
		return mav;
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", new PopUser());
		ModelAndView mav = new ModelAndView("login", map);
		return mav;
	}
	
	@RequestMapping("/userlist")
	public ModelAndView queryAll()
			throws Exception {
		List<PopUser> userlist = popUserService.queryAll();
		for (Iterator<PopUser> iterator = userlist.iterator(); iterator.hasNext();) {
			PopUser popUser = (PopUser) iterator.next();
			System.out.println(popUser.getName());
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userlist", userlist);
		ModelAndView mav = new ModelAndView("userlist", map);
		return mav;
	}
}
